#!/bin/bash 
# Here we figure out DL_SERVER, SOURCE_ROOT, WEB_DIR, etc.
. functions.sh


if !(grep -q $HOST_ENV /etc/resolv.conf); then
  if !(grep -q search  /etc/resolv.conf); then
    echo "search $HOST_ENV" >> /etc/resolv.conf
  else
    sed -i "s/search.*$/& $HOST_ENV/g" /etc/resolv.conf
  fi
fi

if !(grep -q s3.$HOST_ENV /etc/hosts); then
  echo "127.0.0.1 it-s4-fs-7aad91fd-e571-b6e5-571f-cebe20486ced.s3.$HOST_ENV" >> /etc/hosts
fi

echo "--> Shut down firewalld"
sudo systemctl stop firewalld.service
sudo systemctl disable firewalld.service

echo "--> Init Mongodb"
sudo sh /vagrant/vagrant/mongoinit.sh
sudo sh /vagrant/vagrant/apimgt-data-init.sh
sudo sh /vagrant/vagrant/plugininit.sh

echo_and_log "--> Migrating mongodb-migrate ..."
sudo $WEB_DIR/yii mongodb-migrate --interactive=0

echo "--> Change jdk to 1.8"
sudo alternatives --set java /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.60-2.b27.el7_1.x86_64/jre/bin/java
sudo alternatives --set javac /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.60-2.b27.el7_1.x86_64/bin/javac

echo "--> Init Fakes3"
sudo curl -s -X PUT http://it-s4-fs-7aad91fd-e571-b6e5-571f-cebe20486ced.s3.$HOST_ENV:1234

echo "--> Create link for nfs"
if [ ! -e /vagrant/nfs ]; then
sudo mkdir -p /vagrant/nfs
fi
sudo chown -R vagrant:vagrant /vagrant/nfs 
sudo chmod -R 755 /vagrant/nfs

if [ ! -e /opt/foxitcloud ]; then
sudo mkdir -p /opt/foxitcloud
fi

if [ ! -L /opt/foxitcloud/nfs ]; then
sudo ln -s /vagrant/nfs /opt/foxitcloud/nfs
fi

echo "--> Make sure apache has permission to web root"
sudo chown -R vagrant:vagrant /var/lib/foxit_ucx/docoshareapp


echo "--> Disable httpd private tmp"
sudo sed -i "s/PrivateTmp=true/PrivateTmp=false/g" /etc/systemd/system/multi-user.target.wants/httpd.service
sudo systemctl daemon-reload
sudo systemctl restart httpd


echo "--> Restart fxphp server"
sudo systemctl stop fxphp
systemctl disable fxphp.service
systemctl enable fxphp.service
sudo systemctl start fxphp

echo "--> Post_provision finished in ${SECONDS}s"
